using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CodePlex.SpaceBlock;
using CodePlex.SpaceBlock.FS;
using System.Threading;

namespace CodePlex.SpaceBlock.UI.Controls.Explore
{
    public partial class ExploreControl : CodePlex.SpaceBlock.UI.Controls.BaseControl
    {

        private readonly BrowserControl _topBrowser;
        private readonly BrowserControl _bottomBrowser;
        public ExploreControl()
        {
            InitializeComponent();

            // top browser
            _topBrowser = new BrowserControl();
            UIUtils.FillWith(_splitContainerInner.Panel1, _topBrowser);

            // bottom browser
            _bottomBrowser = new BrowserControl();
            UIUtils.FillWith(_splitContainerInner.Panel2, _bottomBrowser);

            // hookup transfer logic between the two
            this.HookupTransferEvents(_topBrowser, _bottomBrowser);
            this.HookupTransferEvents(_bottomBrowser, _topBrowser);
        }

        public void Initialize(FileSystemManager fileSystems)
        {
            _topBrowser.Initialize(fileSystems,0);
            _bottomBrowser.Initialize(fileSystems, 1);

        }

        internal void Select(IFileSystem fs, Path path)
        {

            if (_topBrowser.FileSystem.Equals(fs))
            {
                _topBrowser.SelectPath(path);
            }
            else if (_bottomBrowser.FileSystem.Equals(fs))
            {
                _bottomBrowser.SelectPath(path);
            }
            else
            {
                _bottomBrowser.SelectFileSystem(fs,false);
                _bottomBrowser.SelectPath(path);

            }
        }


        private void HookupTransferEvents(BrowserControl fromBrowser, BrowserControl toBrowser)
        {

            EventHandler handler = delegate
            {

               if (fromBrowser == toBrowser)
               {
                   return;
               }

               if (toBrowser.SelectedFolder.Level == 0)
               {
                   return;
               }

               if (fromBrowser.SelectedPaths.Count == 1 && fromBrowser.SelectedPaths[0].Level == 0)
               {
                   return;
               }


               TransferForm transferForm = new TransferForm();

               transferForm.Show();

               Path toFolder = toBrowser.SelectedFolder;

               transferForm.Initialize(
                   fromBrowser.FileSystem,
                   fromBrowser.SelectedPaths,
                   toBrowser.FileSystem,
                   toFolder);

               transferForm.TransferCompleted += delegate
               {
                   // refresh after xfer
                   toBrowser.SelectedFolder = toFolder;
               };

           };

            fromBrowser.TransferFromRequested += handler;
            toBrowser.TransferToRequested += handler;

        }


      
    }
}

